﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api.Ddl;

namespace Me.Prettyprint.Cassandra.Serializers
{
    /// <summary>
    /// IntegerSerializer uses INT32 type for the comparator. It handles the conversion
    /// between big/little endian encoding to make sure data is cross platform safe.
    /// </summary>
    public class IntegerSerializer : AbstractSerializer<int>
    {
        public static IntegerSerializer Instance = new IntegerSerializer();

        public IntegerSerializer()
        {
        }
        public override ComparatorType GetComparatorType()
        {
            return ComparatorType.INT32TYPE;
        }
        override public ComparatorType ComparatorType
        {
            get
            {
                return ComparatorType.INT32TYPE;
            }
        }
        public override byte[] ToBytes(int obj)
        {
            return ConvertToBigEndianBytes(BitConverter.GetBytes(obj));
        }
        public override Utils.ByteBuffer ToByteBuffer(int obj)
        {
            return new ByteBuffer(ToBytes(obj));
        }
        public override int FromBytes(byte[] bytes)
        {
            return BitConverter.ToInt32(ConvertFromBigEndianBytes(bytes),0);
        }
        public override int FromByteBuffer(ByteBuffer buff)
        {
            return FromBytes(buff.GetBuffer());
        }
    }
}
